package com.honeycomb.action.web;

import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.honeycomb.action.BaseWebAction;
import com.honeycomb.service.IUserService;
import com.honeycomb.utils.HoneyCombColumnIndicator;
import com.honeycomb.utils.HoneyCombTools;
import com.honeycomb.utils.MD5Utils;
import com.honeycomb.utils.SystemConstants;
import com.honeycomb.utils.SystemValidate;
import com.honeycomb.vo.BaseVO;
import com.honeycomb.vo.MapInfoVO;
import com.honeycomb.vo.ResultDto;
import com.honeycomb.vo.UserInfoVO;
import com.honeycomb.vo.UserPasswordVO;
import com.honeycomb.vo.UserRegistVO;
import com.honeycomb.vo.UserStatusVO;


/**
 * 
 * @function: 用户注册
 * 
 * @param userRegistEmail 用户注册邮箱
 * 
 * @param userRegistNickname 用户注册昵称
 * 
 * @param userPassword  用户注册密码
 * 
 * @param userRegistSex 用户性别
 * 
 * @param userRegistBirthday 用户生日
 * 
 * @return String
 * 
 * @author ZhongJiang
 * 
 */
public class DoRegistAction extends BaseWebAction {
	
	private IUserService iUserService;
	
	private String userRegistEmail;
	private String userRegistNickname;
	private String userRegistPassword;
	private String userRegistSex;
	private String userRegistBirthday;
	
	private String mapX;
	private String mapY;
	private String mapAddr;
	private String mapDesc;
	private String mapProvider;
	private String mapType;

	@Override
	protected String doRequest(HttpServletRequest request,
			HttpServletResponse response, HttpSession session) {
		logger.info("------------------DoRegistAction----------------");
		logger.info("getUserRegistEmail:"+this.getUserRegistEmail());
		logger.info("getUserRegistNickname:"+this.getUserRegistNickname());
		logger.info("getUserRegistPassword:"+this.getUserRegistPassword());
		logger.info("getUserRegistSex:"+this.getUserRegistSex());
		logger.info("getUserRegistBirthday:"+this.getUserRegistBirthday());
		
		logger.info("mapX:"+this.getMapX());
		logger.info("mapY:"+this.getMapY());
		logger.info("mapAddr:"+this.getMapAddr());
		logger.info("mapDesc:"+this.getMapDesc());
		logger.info("mapProvider:"+this.getMapProvider());
		logger.info("mapType"+this.getMapType());
		
		ResultDto<BaseVO> resultDtoRegist = null;
		try {
			//邮箱地址的合法性[没有]验证
			
			//验证非空
			if(SystemValidate.isEmpty(this.getUserRegistEmail())
					|| SystemValidate.isEmpty(this.getUserRegistNickname())
					|| SystemValidate.isEmpty(this.getUserRegistPassword())
					|| SystemValidate.isEmpty(this.getUserRegistBirthday())
					|| SystemValidate.isEmpty(this.getUserRegistSex())) {
				return "failed";
			}
			
			//验证字符串长度
			if(!SystemValidate.validateInput(this.getUserRegistNickname(), 4, 10, "0aA")
					|| !SystemValidate.validateInput(this.getUserRegistPassword(), 6, 10, "0aA")) {
				return "failed";
			}
			
			//生日日期验证
			if (!SystemValidate.validateDate(this.getUserRegistBirthday(), SystemConstants.HoneyComb_Date_Pattern_Short)) {
				return "failed";
			}
			
			/*
			 * 此处添加地图信息
			 */
			MapInfoVO mapInfoVO = new MapInfoVO();
			mapInfoVO.setMapId(HoneyCombTools.getUUIDString());
			mapInfoVO.setMapCreateDttm(HoneyCombTools.convertCurrentDTTMtoInt());
			mapInfoVO.setMapCreateIp(this.getRQ().getRemoteAddr());
			mapInfoVO.setMapProvider(this.getMapProvider());
			mapInfoVO.setMapAddr(this.getMapAddr());
			mapInfoVO.setMapDesc(this.getMapDesc());
			mapInfoVO.setMapX(Double.valueOf(this.getMapX()));
			mapInfoVO.setMapY(Double.valueOf(this.getMapY()));
			mapInfoVO.setMapType(Short.parseShort(this.getMapType()));
			
			UserRegistVO userRegistVO = new UserRegistVO();
			userRegistVO.setUserEmail(this.getUserRegistEmail());
			userRegistVO.setUserNickname(this.getUserRegistNickname());
			userRegistVO.setUserPassword(MD5Utils.generateForPassword(this.getUserRegistPassword()));
			userRegistVO.setUserSex(this.getUserRegistSex());
			userRegistVO.setUserBirthday(this.getUserRegistBirthday());
			userRegistVO.setUserRegistIp(this.getRQ().getRemoteAddr());
			userRegistVO.setUserHeadImgUrl(SystemConstants.HoneyComb_Default_Head_Img);
			
			resultDtoRegist = this.getiUserService().doRegist(userRegistVO,mapInfoVO);
			if(resultDtoRegist.isSuccess()) {
				//如果注册成功
				//作立即登录操作
				UserPasswordVO userPasswordVO = new UserPasswordVO();
				userPasswordVO.setUserId(this.getUserRegistEmail());
				userPasswordVO.setPasswordValue(MD5Utils.generateForPassword(this.getUserRegistPassword()));
				ResultDto<UserInfoVO> resultDtoLogin = this.getiUserService().doLogin(userPasswordVO);
				if(resultDtoLogin.isSuccess()) {
					//如果登录成功
					//作更新用户状态
					UserStatusVO userStatusVO = new UserStatusVO();
					userStatusVO.setUserId(this.getUserRegistEmail());
					ResultDto<BaseVO> resultDtoUpdateStatus = this.getiUserService().doUpdateUserStatusToOnline(userStatusVO);
					if(resultDtoUpdateStatus.isSuccess()) {
						//做Session操作
						this.getSSN().setAttribute(SystemConstants.HoneyComb_LoginUser_Session_Name, resultDtoLogin.getFirstResult());
						return "success";
					}else {
						return "failed";
					}
				}else {
					return "failed";
				}
			}else {
				return "failed";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "GlobalErrorPage";
		}
	}
	
	
	
	public String getUserRegistEmail() {
		return userRegistEmail;
	}
	public void setUserRegistEmail(String userRegistEmail) {
		this.userRegistEmail = userRegistEmail;
	}
	public String getUserRegistNickname() {
		return userRegistNickname;
	}
	public void setUserRegistNickname(String userRegistNickname) {
		this.userRegistNickname = userRegistNickname;
	}
	public String getUserRegistPassword() {
		return userRegistPassword;
	}
	public void setUserRegistPassword(String userRegistPassword) {
		this.userRegistPassword = userRegistPassword;
	}
	public String getUserRegistSex() {
		return userRegistSex;
	}
	public void setUserRegistSex(String userRegistSex) {
		this.userRegistSex = userRegistSex;
	}
	public String getUserRegistBirthday() {
		return userRegistBirthday;
	}
	public void setUserRegistBirthday(String userRegistBirthday) {
		this.userRegistBirthday = userRegistBirthday;
	}
	public IUserService getiUserService() {
		return iUserService;
	}
	public void setiUserService(IUserService iUserService) {
		this.iUserService = iUserService;
	}
	public String getMapX() {
		return mapX;
	}
	public void setMapX(String mapX) {
		this.mapX = mapX;
	}
	public String getMapY() {
		return mapY;
	}
	public void setMapY(String mapY) {
		this.mapY = mapY;
	}
	public String getMapAddr() {
		return mapAddr;
	}
	public void setMapAddr(String mapAddr) {
		this.mapAddr = mapAddr;
	}
	public String getMapDesc() {
		return mapDesc;
	}
	public void setMapDesc(String mapDesc) {
		this.mapDesc = mapDesc;
	}
	public String getMapProvider() {
		return mapProvider;
	}
	public void setMapProvider(String mapProvider) {
		this.mapProvider = mapProvider;
	}
	public String getMapType() {
		return mapType;
	}
	public void setMapType(String mapType) {
		this.mapType = mapType;
	}
	
	

}













